package cn.huiyunche.base.service.interfaces.impl;

import cn.huiyunche.base.service.interfaces.SOrderStowageService;
import cn.huiyunche.base.service.mappers.SOrderStowageMapper;
import cn.huiyunche.base.service.model.SOrderStowage;
import cn.huiyunche.base.service.model.SOrderStowageExample;
import org.apache.commons.collections4.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SOrderStowageImpl implements SOrderStowageService {

    private static final Logger LOGGER = LoggerFactory.getLogger(SOrderStowageImpl.class);

    @Autowired
    private SOrderStowageMapper sOrderStowageMapper;

    @Override
    public Long add(Long customerOrderId, Long stowageModelId) {
        LOGGER.info("add params : {}, {}", customerOrderId, stowageModelId);
        if (null == customerOrderId) {
            LOGGER.error("add customerOrderId must not be null");
            throw new IllegalArgumentException("客户订单id不能为空");
        }
        if (null == stowageModelId) {
            LOGGER.error("add stowageModelId must not be null");
            throw new IllegalArgumentException("配置模型id不能为空");
        }

        SOrderStowage orderStowage = new SOrderStowage();
        orderStowage.setCustomerOrderId(customerOrderId);
        orderStowage.setStowageModelId(stowageModelId);

        sOrderStowageMapper.insertSelective(orderStowage);
        return orderStowage.getId();
    }

    @Override
    public SOrderStowage getByCustomerOrderId(Long customerOrderId) {
        LOGGER.info("add param : {}", customerOrderId);
        if (null == customerOrderId) {
            LOGGER.error("add customerOrderId must not be null");
            throw new IllegalArgumentException("客户订单id不能为空");
        }

        SOrderStowageExample example = new SOrderStowageExample();
        example.createCriteria().andCustomerOrderIdEqualTo(customerOrderId);
        List<SOrderStowage> list = sOrderStowageMapper.selectByExample(example);
        if (CollectionUtils.isNotEmpty(list)) {
            return list.get(0);
        }
        return null;
    }


}
